package com.bwie.system.init;

import com.bwie.common.domain.RedPacketReceived;
import com.bwie.system.mapper.EsRedPacketReceivedMapper;
import com.bwie.system.mq.domain.EsRedPacketReceived;
import com.bwie.system.service.EsRedPacketReceivedService;
import com.bwie.system.service.RedPacketReceivedService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author Junchen_N1
 * @Date 2025/6/17 16:57
 * @ClassName InitSnatchMsgToEs
 * @Description TODO
 */
@Component
@Slf4j
public class InitSnatchMsgToEs {

    @Autowired
    private RedPacketReceivedService redPacketReceivedService;

    @Autowired
    private EsRedPacketReceivedService esRedPacketReceivedService;

    @Autowired
    private EsRedPacketReceivedMapper esRedPacketReceivedMapper;

    @PostConstruct
    public void initSnatchMsgToEs(){
        List<RedPacketReceived> list = redPacketReceivedService.list();
        List<EsRedPacketReceived> esRedPacketReceivedList = new ArrayList<>();
        list.forEach(item->{
            EsRedPacketReceived esRedPacketReceived = new EsRedPacketReceived();
            BeanUtils.copyProperties(item,esRedPacketReceived);
            esRedPacketReceivedList.add(esRedPacketReceived);
        });

        //清空es索引
        esRedPacketReceivedService.flushEs();

        //重新同步
        esRedPacketReceivedMapper.insertBatch(esRedPacketReceivedList);
    }

}
